home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Games / InputSprocketPPTest / ISpTestMain.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  11.0 KB  |  503 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ISpTestMain.cp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     File Ownership:
  11.  
  12.         DRI:                xxx put dri here xxx
  13.  
  14.         Other Contact:        xxx put other contact here xxx
  15.  
  16.         Technology:            xxx put technology here xxx
  17.  
  18.     Writers:
  19.  
  20.         (BWS)    Brent Schorsch
  21.         (sjb)    Steve Bollinger
  22.  
  23.     Change History (most recent first):
  24.  
  25.        <SP9>    10/20/98    BWS        Pause and Quit are now separate needs
  26.          <8>     7/17/98    BWS        fix for new ISpNeedsStruct
  27.          <7>     6/18/98    sjb        InputSprocket.h comes from <> place
  28. */
  29.  
  30. /*************************************************************************************
  31.  
  32. File:      ISpTestMain.cp
  33.  
  34. Copyright © 1996, 1997, 1998 Apple Computer, Inc., All Rights Reserved
  35.  
  36.  
  37. You may incorporate this sample code into your applications without
  38. restriction, though the sample code has been provided "AS IS" and the
  39. responsibility for its operation is 100% yours.  However, what you are
  40. not permitted to do is to redistribute the source as "DSC Sample Code"
  41. after having made changes. If you're going to re-distribute the source,
  42. we require that you make it clear in the source that the code was
  43. descended from Apple Sample Code, but that you've made changes.
  44.  
  45. *************************************************************************************/
  46.  
  47. #include "ISpTestMain.h"
  48. #include <Types.h>
  49. #include <InputSprocket.h>
  50.  
  51. #include <LGrowZone.h>
  52. #include <LWindow.h>
  53. #include <PP_Messages.h>
  54. #include <PP_Resources.h>
  55. //#include <PPobClasses.h>
  56. #include <UDrawingState.h>
  57. #include <UMemoryMgr.h>
  58. #include <URegistrar.h>
  59. #include <LEditField.h>
  60. #include <Events.h>
  61.  
  62. #include "ISpTestGlobals.h"
  63. #include "ISpEventPane.h"
  64. #include "ISpElementView.h"
  65. #include "ISpLQuitWindow.h"
  66.  
  67. // put declarations for resource ids (ResIDTs) here
  68.  
  69. const ResIDT    window_Sample        = 1;    // EXAMPLE
  70.  
  71. const OSType kCreatorCode        = 'ISPP';
  72. const OSType kSubCreatorCode    = '0001';    // we'll use this as versioning for needs list
  73.  
  74. #define    kResourceID_isap                        128
  75. #define    kResourceID_setl                        128
  76.  
  77. Boolean EventProc(EventRecord *inEvent);
  78. Boolean EventProc(EventRecord *inEvent)
  79. {
  80.     if (gMyApp == nil) { return false; }
  81.     
  82.     if (inEvent->what == updateEvt)
  83.     {
  84.         gMyApp->DispatchEvent(*inEvent);
  85.     }
  86.  
  87.     return false;
  88. }
  89.  
  90. // ===========================================================================
  91. //        • Main Program
  92. // ===========================================================================
  93.  
  94. void main(void)
  95. {
  96.     OSStatus err;
  97.     
  98.     err = ISpGetGlobalElementList(&gElementList);
  99.     
  100.     err = ISpElementList_Extract(    gElementList,
  101.                                     kMaxGlobalElements, 
  102.                                     &gNumElements,
  103.                                     gElementBuffer);
  104.                             
  105.     gElement = gElementBuffer[gCurElement];
  106.     
  107.     err = ISpElementList_New(0, 0, &gElementList, 0);
  108.     
  109.     gStatus = err;
  110.         
  111.                                 // Set Debugging options
  112.     SetDebugThrow_(debugAction_Alert);
  113.     SetDebugSignal_(debugAction_Alert);
  114.  
  115.     InitializeHeap(3);                // Initialize Memory Manager
  116.                                     // Parameter is number of Master Pointer
  117.                                     //   blocks to allocate
  118.     
  119.                                     // Initialize standard Toolbox managers
  120.     UQDGlobals::InitializeToolbox(&qd);
  121.     
  122.     new LGrowZone(20000);            // Install a GrowZone function to catch
  123.                                     //    low memory situations.
  124.  
  125.     CPPStarterApp    theApp;            // replace this with your App type
  126.     gMyApp = &theApp;
  127.     theApp.Run();
  128.     
  129. }
  130.  
  131.  
  132. // ---------------------------------------------------------------------------
  133. //        • CPPStarterApp             // replace this with your App type
  134. // ---------------------------------------------------------------------------
  135. //    Constructor
  136.  
  137. CPPStarterApp::CPPStarterApp()
  138. {
  139.     URegistrar::RegisterClass(ISpElementView::class_ID, (ClassCreatorFunc) ISpElementView::CreateISpElementViewStream);
  140.     URegistrar::RegisterClass(ISpEventPane::class_ID, (ClassCreatorFunc) ISpEventPane::CreateISpEventPaneStream);
  141.     URegistrar::RegisterClass(LQuitWindow::class_ID, (ClassCreatorFunc) LQuitWindow::CreateLQuitWindowStream);
  142.     // Register functions to create core PowerPlant classes
  143.     
  144. //    RegisterAllPPClasses();
  145. }
  146.  
  147.  
  148. // ---------------------------------------------------------------------------
  149. //        • ~CPPStarterApp            // replace this with your App type
  150. // ---------------------------------------------------------------------------
  151. //    Destructor
  152. //
  153.  
  154. CPPStarterApp::~CPPStarterApp()
  155. {
  156.     ISpElement_DisposeVirtual(kNumNeeds, mVirtualElements);
  157. }
  158.  
  159. // ---------------------------------------------------------------------------
  160. //        • StartUp
  161. // ---------------------------------------------------------------------------
  162. //    This function lets you do something when the application starts up. 
  163. //    For example, you could issue your own new command, or respond to a system
  164. //  oDoc (open document) event.
  165.  
  166. void
  167. CPPStarterApp::StartUp()
  168. {
  169.     InitNeeds();
  170.  
  171.     LWindow *theWindow;
  172.     
  173.     theWindow = LWindow::CreateWindow(1, this);    
  174.     theWindow->Show();
  175.  
  176.     theWindow = LWindow::CreateWindow(2, this);    
  177.     theWindow->Show();
  178.  
  179.  
  180. }
  181. static void EnableDeviceClass(OSType deviceClass, Boolean enable)
  182. {
  183.     enum {
  184.         kDeviceList_COUNT = 100
  185.     };
  186.     
  187.     OSStatus err;
  188.     UInt32 count;
  189.     ISpDeviceReference deviceList[kDeviceList_COUNT];
  190.     
  191.     // NOTE: This is not the correct way to handle the list count thing.  We
  192.     // should actually call once with NULL for the device list, malloc a list of
  193.     // that size, and call again.
  194.     
  195.     err = ISpDevices_ExtractByClass(
  196.             deviceClass,
  197.             kDeviceList_COUNT,
  198.             &count,
  199.             deviceList);
  200.     
  201.     if (err)
  202.     {
  203.         gStatus = err;
  204.         return;
  205.     }
  206.     
  207.     if (count > kDeviceList_COUNT)
  208.     {
  209.         count = kDeviceList_COUNT;
  210.     }
  211.     
  212.     if (enable)
  213.     {
  214.         err = ISpDevices_Activate(
  215.                 count,
  216.                 deviceList);
  217.         
  218.         if (err)
  219.         {
  220.             gStatus = err;
  221.             return;
  222.         }
  223.     }
  224.     else
  225.     {
  226.         err = ISpDevices_Deactivate(
  227.                 count,
  228.                 deviceList);
  229.         
  230.         if (err)
  231.         {
  232.             gStatus = err;
  233.             return;
  234.         }
  235.     }
  236.         
  237.     gStatus = err;
  238. }
  239.  
  240. // ---------------------------------------------------------------------------
  241. //        • ObeyCommand
  242. // ---------------------------------------------------------------------------
  243. //    Respond to commands
  244.  
  245. Boolean
  246. CPPStarterApp::ObeyCommand(
  247.     CommandT    inCommand,
  248.     void        *ioParam)
  249. {
  250.     Boolean        cmdHandled = true;
  251.  
  252.     switch (inCommand) {
  253.     
  254.         // Deal with command messages (defined in PP_Messages.h).
  255.         // Any that you don't handle will be passed to LApplication
  256.              
  257.         case cmd_New:
  258.                                         // EXAMPLE, create a new window
  259.             break;
  260.  
  261.  
  262.         case cmd_Suspend:
  263.             gStatus = ISpSuspend();
  264.             break;
  265.             
  266.         case cmd_Resume:
  267.             gStatus = ISpResume();
  268.             break;
  269.         
  270.         case cmd_NextElement:
  271.             gStatus = noErr;
  272.             gCurElement = (gCurElement + 1) % gNumElements;
  273.             gElement = gElementBuffer[gCurElement];
  274.             break;
  275.             
  276.         case cmd_PrevElement:
  277.             gStatus = noErr;
  278.             gCurElement = (gCurElement + gNumElements - 1) % gNumElements;
  279.             gElement = gElementBuffer[gCurElement];
  280.             break;
  281.  
  282.         case cmd_AddElement:
  283.             gStatus = ISpElementList_AddElements(    gElementList, 
  284.                                                     gCurElement,
  285.                                                     1,
  286.                                                     &gElement);
  287.             break;
  288.  
  289.         case cmd_DeleteElement:
  290.             gStatus = ISpElementList_RemoveElements(    gElementList, 
  291.                                                         1,
  292.                                                         &gElement);
  293.             break;
  294.  
  295.         case cmd_UIStart:
  296.             gStatus = ISpInit(    kNumNeeds,
  297.                                 mNeeds,
  298.                                 mVirtualElements,
  299.                                 kCreatorCode,
  300.                                 kSubCreatorCode,
  301.                                 0,    // flags
  302.                                 kResourceID_setl,
  303.                                 0); // reserved
  304.             break;
  305.             
  306.         case cmd_UIConfigure:
  307.             gStatus = ISpConfigure(&EventProc);
  308.             break;
  309.             
  310.         case cmd_UIStop:
  311.             gStatus = ISpStop();
  312.             break;
  313.     
  314.         case cmd_ActivateKeyboard:
  315.             EnableDeviceClass(kISpDeviceClass_Mouse, false);
  316.             EnableDeviceClass(kISpDeviceClass_Keyboard, true);
  317.             break;
  318.     
  319.         case cmd_ActiveMouse:
  320.             EnableDeviceClass(kISpDeviceClass_Keyboard, false);
  321.             EnableDeviceClass(kISpDeviceClass_Mouse, true);
  322.             break;
  323.             
  324.         case cmd_DeactiveBoth:
  325.             EnableDeviceClass(kISpDeviceClass_Mouse, false);
  326.             EnableDeviceClass(kISpDeviceClass_Keyboard, false);
  327.             break;
  328.  
  329.             
  330.         default:
  331.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  332.             break;
  333.     }
  334.     
  335.     return cmdHandled;
  336. }
  337.  
  338. // ---------------------------------------------------------------------------
  339. //        • FindCommandStatus
  340. // ---------------------------------------------------------------------------
  341. //    This function enables menu commands.
  342. //
  343.  
  344. void
  345. CPPStarterApp::FindCommandStatus(
  346.     CommandT    inCommand,
  347.     Boolean        &outEnabled,
  348.     Boolean        &outUsesMark,
  349.     SInt16        &outMark,
  350.     Str255        outName)
  351. {
  352.  
  353.     switch (inCommand) {
  354.     
  355.         // Return menu item status according to command messages.
  356.         // Any that you don't handle will be passed to LApplication
  357.  
  358.         case cmd_New:                    // EXAMPLE
  359.             outEnabled = false;
  360.             break;
  361.             
  362.         case cmd_Suspend:
  363.         case cmd_Resume:
  364.         case cmd_UIStart:
  365.         case cmd_UIConfigure:
  366.         case cmd_UIStop:
  367.             outEnabled = false;
  368.             break;
  369.             
  370.         case cmd_NextElement:
  371.         case cmd_PrevElement:
  372.         case cmd_AddElement:
  373.         case cmd_DeleteElement:
  374.         case cmd_ActivateKeyboard:
  375.         case cmd_ActiveMouse:
  376.         case cmd_DeactiveBoth:
  377.             outEnabled = true;            // enable the New command
  378.             break;
  379.  
  380.         default:
  381.             LApplication::FindCommandStatus(inCommand, outEnabled,
  382.                                                 outUsesMark, outMark, outName);
  383.             break;
  384.     }
  385. }
  386.  
  387. void CPPStarterApp::EventResume        (const EventRecord &inMacEvent)
  388. {
  389.     LApplication::EventResume(inMacEvent);
  390.  
  391.     ISpResume();
  392. }
  393.  
  394. void CPPStarterApp::EventSuspend    (const EventRecord &inMacEvent)
  395. {
  396.     ISpSuspend();
  397.     
  398.     LApplication::EventSuspend(inMacEvent);
  399. }
  400.  
  401. void CPPStarterApp::InitNeeds(void)
  402. {
  403.     ISpNeed tempNeeds[kNumNeeds] =
  404.     {
  405.         {
  406.             "\pForward Thrust",
  407.             kIconSuiteID_YThrust,
  408.             0,        // player number
  409.             0,        // group number
  410.             kISpElementKind_Axis,
  411.             kISpElementLabel_Axis_YAxis,
  412.             0,        // flags
  413.             0,        // reserved 1
  414.             0,        // reserved 2
  415.             0        // reserved 3
  416.         },
  417.         {
  418.             "\pSide Thrust",
  419.             kIconSuiteID_XThrust, 
  420.             0,        // player number
  421.             0,        // group number
  422.             kISpElementKind_Axis,
  423.             kISpElementLabel_Axis_XAxis,
  424.             0,        // flags
  425.             0,        // reserved 1
  426.             0,        // reserved 2
  427.             0        // reserved 3
  428.         },
  429.         {
  430.             "\pVertical Thrust",
  431.             kIconSuiteID_ZThrust, 
  432.             0,        // player number
  433.             0,        // group number
  434.             kISpElementKind_Axis,
  435.             kISpElementLabel_Axis_ZAxis,
  436.             0,        // flags
  437.             0,        // reserved 1
  438.             0,        // reserved 2
  439.             0        // reserved 3
  440.         },
  441.         {
  442.             "\pLook", 
  443.             kIconSuiteID_Look, 
  444.             0,        // player number
  445.             0,        // group number
  446.             kISpElementKind_Movement,
  447.             kISpElementLabel_None,
  448.             0,        // flags
  449.             0,        // reserved 1
  450.             0,        // reserved 2
  451.             0        // reserved 3
  452.         },
  453.         {
  454.             "\pFire", 
  455.             kIconSuiteID_Fire, 
  456.             0,        // player number
  457.             0,        // group number
  458.             kISpElementKind_Button,
  459.             kISpElementLabel_Btn_Fire,
  460.             0,        // flags
  461.             0,        // reserved 1
  462.             0,        // reserved 2
  463.             0        // reserved 3
  464.         },
  465.         {
  466.             "\pStart/Pause", 
  467.             kIconSuiteID_Pause, 
  468.             0,        // player number
  469.             0,        // group number
  470.             kISpElementKind_Button,
  471.             kISpElementLabel_Btn_StartPause,
  472.             kISpNeedFlag_NoMultiConfig,    // flags
  473.             0,        // reserved 1
  474.             0,        // reserved 2
  475.             0        // reserved 3
  476.         },
  477.         {
  478.             "\pQuit", 
  479.             kIconSuiteID_Start,
  480.             0,        // player number
  481.             0,        // group number
  482.             kISpElementKind_Button,
  483.             kISpElementLabel_Btn_Quit,
  484.             kISpNeedFlag_NoMultiConfig,        // flags
  485.             0,        // reserved 1
  486.             0,        // reserved 2
  487.             0        // reserved 3
  488.         },
  489.     };
  490.     
  491.  
  492.     int itr;
  493.     for(itr = 0; itr < kNumNeeds; itr++)
  494.     {
  495.         mNeeds[itr] = tempNeeds[itr];
  496.     }
  497.  
  498.     OSStatus err;
  499.       err = ISpElement_NewVirtualFromNeeds(kNumNeeds, mNeeds, mVirtualElements, 0);
  500.       
  501.       if (err != noErr) { DebugStr("\pISpElement_NewVirtualFromNeeds failed!"); }
  502.  
  503. }